package view
{
    import flash.display.*;

    public class Airplane extends Sprite
    {
        private static const SPEED:Number = 0.2;
        private static const MAX_HP:int = 100;
        private static const MAX_AP:int = 3;

        public var healthPoint:int;
        public var attackPoint:int;
        public var fire:Boolean;

        private var _hit:Boolean;

        public function Airplane()
        {
            healthPoint = MAX_HP;
            attackPoint = MAX_AP;
            fire = false;
            _hit = false;
        }

        public function get hit():Boolean
        {
            return _hit;
        }

        public function set hit(v:Boolean):void
        {
            if(_hit != v)
            {
                _hit = v;
                if(_hit)
                {
                    display(0xFFFFFF);
                }
                else
                {
                    display();
                }
            }
        }

        public function reset():void
        {
            healthPoint = MAX_HP;
            attackPoint = MAX_AP;
            fire = false;
        }

        public function display(c:uint=0x80C0FF):void
        {
            graphics.clear();
            graphics.beginFill(c);
            graphics.moveTo(0, 0);
            graphics.lineTo(24, 16);
            graphics.lineTo(-24, 16);
            graphics.endFill();
            
            graphics.beginFill(c);
            graphics.moveTo(0, 16);
            graphics.lineTo(8, 24);
            graphics.lineTo(-8, 24);
            graphics.endFill();
            
            graphics.beginFill(0x00A0FF);
            graphics.moveTo(0, -24);
            graphics.lineTo(4, 10);
            graphics.lineTo(0, 28);
            graphics.lineTo(-4, 10);
            graphics.endFill();
        }

        public function update(t:Number, toX:Number, toY:Number):void
        {
            var dx:Number = toX - x;
            var dy:Number = toY - y;
            var l:Number = Math.sqrt((dx*dx) + (dy*dy));
            if(l < t * SPEED)
            {
                x = toX;
                y = toY;
            }
            else
            {
                x += (t * SPEED) * dx / l;
                y += (t * SPEED) * dy / l;
            }

            if(x < -180)
            {
                x = -180;
            }
            if(x > 180)
            {
                x = 180;
            }
            if(y < -200)
            {
                y = -200;
            }
            if(y > 180)
            {
                y = 180;
            }
        }
    }
}
